angular.module('app').controller('integralCtrl',['$scope' ,'utils' ,'$http','$rootScope','$q','$state','activity'
    ,function ($scope ,utils,$http,$rootScope,$q,$state,activity) {

    $rootScope.availableBonus=0;
    /**
     * 获取商品类别
     * @returns {Promise}
     */
    function getGoodsType() {
        var def = $q.defer();
        $http.get(utils.integralSys, {
            params: {
                header: utils.header("300122"),
                body: {
                }
            }
        }).then(function (res) {
                if(res.header.re_code == "0000") {
                    def.resolve(res.body.goodsTypeList)
                }
        });
        return def.promise;
    }
        if($scope.isapp){
            appload={
                CallBack:function (type) {

                }
            };
        }

        $scope.$on('$viewContentLoaded', function(event) {
            setTimeout(function () {
                if($rootScope.isapp) {
                    wv.setHeader('积分商城','1','商城规则','1')
                }
            },300)
        });


    /**
     * 获取用户积分
     */
    function getIntegral() {
        var  solocode= activity.getSoloCode();
       if(solocode){
            $http.get(utils.integralSys, {
                params: {
                    header: utils.header("300312"),
                    body: {
                        userCode:solocode,
                    }
                }
            }).then(function (res) {
                if(res.header.re_code == "0000") {
                    $rootScope.availableBonus=res.body.availableBonus;
                }
            })
        }else {
            mui.alert('请您登录后继续操作', function() {
               if($rootScope.isapp){
                   wv.go('2');
               }else {
                   $state.go('register');
               }
            });
        }
    }

    $scope.toIntDetail= function(){
        $state.go('integral_details')
    };


      getGoodsType().then(function (res) {
         for(i=0;i<res.length;i++){
             if(res[i]){
                 if(res[i].goods.length<=0){
                     res[i].itemShow=false;
                 }else {
                     res[i].itemShow=true;
                 }
             }else {
                 res[i].itemShow=false;
             }
        }
          $scope.rows=res;
      });
       getIntegral();



        /**
         * 是否售完
         * @param goods
         * @returns {boolean}
         */
        $scope.isSelloutGoods=function (remaining) {
            $scope.remaining=remaining;
            if(remaining>0){
                return false;
            }else {
                return true;
            }
        }
        $scope.$watch('remaining',function(){
            $scope.isSelloutGoods($scope.remaining);
        });
        /**
         * 详情页
         * @param goodsCode
         */
        $scope.toDetail=function (good) {
            if(good.goodsCode){
                $state.go('integral_product_details',{id:good.goodsCode ,li:good.limitType})
            }
        }


    
}])